package com.ymatou.iapi.openapibiz.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.openapibiz.parameter.AuthorizeBean;
import com.ymatou.iapi.openapibiz.parameter.CancelAuthBean;
import com.ymatou.iapi.openapibiz.parameter.NewThirdPartyAppBean;
import com.ymatou.iapi.openapibiz.service.AuthorizeCall;
import com.ymatou.iapi.openapibiz.service.CancelAuthCall;
import com.ymatou.iapi.openapibiz.service.NewThirdPartyAppCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.OpenApiBizCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sqlwapper.OpenApiWapper;

/**
*取消授权app
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CancelAuth{
	private static CancelAuthBean cancelauthBean;
	private static CancelAuthCall cancelauthCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("取消授权app");
	}

	@Before
	public void caseUp() {
		cancelauthBean=new CancelAuthBean();
		cancelauthCall=new CancelAuthCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cancelauthCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_001() {
		Logger.start(true,"取消授权-自主开发");
		try {
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);			
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(authorizeBean.getAppName()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
			
			cancelauthBean.setSellerId(authorizeBean.getSellerId());
			cancelauthBean.setAuthCode(authCode.getAuth_code());
			cancelauthBean.setAppId(application.getApp_id());

			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();
			
			Logger.verifyEquals("200", cancelauthCall.getCode(), "验证Code");
			Logger.verifyEquals("取消授权成功!", cancelauthCall.getMessage(), "验证Message");	
			
			AuthCode authCode1 = new OpenApiWapper().selectAuthCodeById(authCode.getId()).get(0);
			Logger.verifyEquals("DISABLE", authCode1.getStatus(), "验证Auth_Code表Status");	
			
			Application application1 = new OpenApiWapper().selectApplicationById(application.getApp_id()).get(0);
			Logger.verifyEquals("DISABLE", application1.getStatus(), "验证Application表Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_002() {
		Logger.start(true,"取消授权-第三方应用");
		try {
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			NewThirdPartyAppBean newThirdPartyAppBean = OpenApiBizCallService.createNewThirdPartyAppBean();
			NewThirdPartyAppCall newThirdPartyAppCall = OpenApiBizCallService.newthirdpartyappCall(newThirdPartyAppBean);
	
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_THIRD_PARTY(newThirdPartyAppCall.getAppId());
			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationById(authorizeBean.getAppId()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
			
			cancelauthBean.setSellerId(authorizeBean.getSellerId());
			cancelauthBean.setAuthCode(authCode.getAuth_code());
			cancelauthBean.setAppId(application.getApp_id());

			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();					
			
			Logger.verifyEquals("200", cancelauthCall.getCode(), "验证Code");
			Logger.verifyEquals("取消授权成功!", cancelauthCall.getMessage(), "验证Message");	
			
			AuthCode authCode1 = new OpenApiWapper().selectAuthCodeById(authCode.getId()).get(0);
			Logger.verifyEquals("DISABLE", authCode1.getStatus(), "验证Auth_Code表Status");	
	
			Application application1 = new OpenApiWapper().selectApplicationById(application.getApp_id()).get(0);
			Logger.verifyEquals("ENABLE", application1.getStatus(), "验证Application表Status");	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_003() {
		Logger.start(true,"sellerId不传");
		try {
			cancelauthBean.setAuthCode("eeee");
			cancelauthBean.setAppId("ewewfew");

			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();	
			
			Logger.verifyEquals("400", cancelauthCall.getCode(), "验证Code");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_005() {
		Logger.start(true,"appId传空");
		try {
			cancelauthBean.setSellerId(33333);
			cancelauthBean.setAuthCode("eeee");
		
			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();			
			
			Logger.verifyEquals("400", cancelauthCall.getCode(), "验证Code");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_006() {
		Logger.start(true,"appId不存在");
		try {

			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(authorizeBean.getAppName()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
			
			cancelauthBean.setSellerId(authorizeBean.getSellerId());
			cancelauthBean.setAuthCode(authCode.getAuth_code());
			cancelauthBean.setAppId("dddd");

			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();			

			Logger.verifyEquals("400", cancelauthCall.getCode(), "验证Code");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_007() {
		Logger.start(true,"authCode不传");
		try {
			cancelauthBean.setSellerId(33333);
			cancelauthBean.setAppId("ewewfew");
			
			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();				
			
			Logger.verifyEquals("400", cancelauthCall.getCode(), "验证Code");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelAuth_008() {
		Logger.start(true,"authCode不正确");
		try {
			
			OpenApiBizCallService OpenApiBizCallService = new OpenApiBizCallService();
			
			AuthorizeBean authorizeBean = OpenApiBizCallService.createAuthorizeBean_USER_DEV();
			AuthorizeCall authorizeCall = OpenApiBizCallService.authorizeCall(authorizeBean);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper.selectApplicationByAppName(authorizeBean.getAppName()).get(0);
			AuthCode authCode =  openApiWapper.selectAuthCodeByAppId(application.getApp_id()).get(0);		
			
			cancelauthBean.setSellerId(authorizeBean.getSellerId());
			cancelauthBean.setAuthCode(authCode.getAuth_code()+"dd");
			cancelauthBean.setAppId(application.getApp_id());
			
			cancelauthCall.setData(cancelauthBean);
			cancelauthCall.callService();			

			Logger.verifyEquals("400", cancelauthCall.getCode(), "验证Code");
	
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}